/** @jsxImportSource @emotion/react */
import * as React from "react";
import {InspectRecordLayout, InternalItemProps, OriginalViewProps, useItemInputControl,} from "../../common/base";
import {assertNamesUnique, createItem,} from "../../common/eHelper";
import {useRecordListSubr} from "../../hook/useRecordListSub";
import {DeviceSurvey} from "../../elevator/editor";
import {useMedia} from "use-media";
import {Button, Text, TextArea, useToast} from "customize-easy-ui-component";
import {EditStorageContext} from "../../StorageContext";
import {itemA结论, ItemConclusion} from "../offline/orcBase";

const PA8介质=['空气','氮气','蒸汽','水','其他'];
const PA2阀类型=['弹簧式','先导式','重锤式','静重式','杠杆式','脉冲式'];
const PA17校验方式=['离线校验','在线实跳','在线助跳',];
const 校验装置名称=['离线校验台',];

export const config证书概要 = [
  [['使用单位', '使用单位'], ['联系电话', '安管电话']],
  [['单位地址', {n:'使用单位地址',t:'B'}], ['联系人', '安管人员']],
  //使用单位 :?丛单位台账做挑选？相关选定对象信息转移
  [['设备代码', '设备代码'], ['设备安装位置', '设备位置']],
  [['所在设备名称', '所在设备'], ['所在设备编号', '所在设备号']],
  [['设备工作压力', {n:'设备压力',u:'MPa' }], ['设备工作介质', {n:'设备介质',t:'l',l:PA8介质}]],
  [['安全阀类型', {n:'阀类型',t:'l',l:PA2阀类型}], ['安全阀安装位置','阀安装位'] ],
  [['制造单位名称', '制造单位'], ['安全阀型号','阀型号']],
  [['公称通径', '_$公称通径','mm'], ['校验介质',{n:'验介质',t:'l',l:PA8介质} ]],
  [['公称压力', '_$公称压力','MPa'], ['校验方式',{n:'验方式',t:'l',l:PA17校验方式} ]],
  [['委托整定压力', {n:'委整定压',u:'MPa' }], ['校验装置编号', '校验装号']],
  [['实际整定压力', {n:'实整定压',u:'MPa' }], ['校验装置名称', {n:'校验装',t:'l',l:校验装置名称} ]],
  [['密封试验压力', {n:'密试验压',u:'MPa' }], ['铅封标记', '铅封']],
  [['标牌号', '标牌号'], ['一品一码', '一品码']],
  [['安全阀出厂编号', '出厂编号'], ],
];


export const EntranceSetup =
    React.forwardRef((
        {children, show, alone = true, repId, verId, rep}: InternalItemProps, ref
    ) => {
      const atPrint = useMedia('print');
      const toast = useToast();
      const doCheckNames = React.useCallback((verId: string) => {
        //不需要台账 {value: rep?.tzFields},
        const result = assertNamesUnique([
          {value: config证书概要, type:'esnt'},
          {value:[...itemA结论,  ]} ]);
        toast({title: "完成！", subtitle: result ? "没发现冲突" : "测试开关没开", intent: "success"});
      }, [rep,toast]);
      const {storage, setStorage} =React.useContext(EditStorageContext) as any;
      const getInpFilter = React.useCallback((par: any) => {
        const {_tblFixed, } =par||{};
        return {_tblFixed, };
      }, []);
      const {inp, setInp} = useItemInputControl({ref});
      if (atPrint) return null;
      else return (
          <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                               alone={alone} label={'初始化本报告，默认值配置等'}>
            {process.env.REACT_APP_TEST==='true' && <><div>
              <Text variant="h5">构建开发模板时的工具：校验模板的存储name冲突；</Text>
              <Button intent='primary' onPress={() => doCheckNames(verId!)}>校验模板name唯一性</Button>
              <Text variant="h5">设置待测试表格的各列宽度：</Text>
              <TextArea value={storage?._tblFixed || ''} rows={2}
                        onChange={e => setStorage({ ...storage, _tblFixed: e.currentTarget.value || undefined}) }/>
            </div>
            </>
            }
            <hr/>
          </InspectRecordLayout>
      );
    });



const recordPrintList =[
  createItem('Entrance', <EntranceSetup/>),
  createItem('CertificateSummary', <DeviceSurvey config={config证书概要} label={'安全阀参数概要'}/>),
  createItem('Conclusion', <ItemConclusion/>),
];

export const OriginalView=
  React.forwardRef((
    { action: modAction,  verId, repId='', rep}
    :OriginalViewProps, ref
  ) => {
    const {list}=useRecordListSubr(ref,rep,recordPrintList,modAction,verId);
    return <React.Fragment>
      {list}
    </React.Fragment>;
  } );
